package com.rbac.api.controller.lessOne;

import com.rbac.api.controller.request.*;
import com.rbac.api.service.UsersCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

///管理员相关controller
@RestController
public class UsersController {


    //管理员相关操作
    @Autowired
    private UsersCommonService userCommonService;


    ///登录
    @RequestMapping(value = "/admin/public/login",method = RequestMethod.POST)
    public Object userLoginByAccountAndPwd(@Valid @RequestBody PostUserLoginRequest userLoginRequest, BindingResult bindingResult,HttpServletRequest request) throws Exception {
        //如果校验存在错误
        if(bindingResult.hasErrors()){
            throw new MethodArgumentNotValidException(null,bindingResult);
        }

        //登录
        return userCommonService.commonUsersLoginService(userLoginRequest,request);
    }

    ///退出
    @ResponseBody
    @RequestMapping(value = "/admin/common/out",method = RequestMethod.POST)
    public Object userOutByToken(HttpServletRequest request) throws Exception{
        //
        return userCommonService.commonUsersOutService(request);
    }


    ///管理员修改密码
    @ResponseBody
    @RequestMapping(value = "/admin/common/users/savePwd",method = RequestMethod.POST)
    public Object userSavePassword(@Valid @RequestBody PostUserSavePwdRequest userSavePwdRequest, BindingResult bindingResult, HttpServletRequest request) throws Exception{
        //如果校验存在错误
        if(bindingResult.hasErrors()){
            throw new MethodArgumentNotValidException(null,bindingResult);
        }
        ///
        return userCommonService.userSavePassword(userSavePwdRequest,request);
    }




    ///获取管理员列表
    @ResponseBody
    @RequestMapping(value = "/admin/check/users/getList",method = RequestMethod.GET)
    public Object getUsersList(@Valid PostUsersListRequest usersListReques,BindingResult bindingResult) throws Exception{
        //如果校验存在错误
        if(bindingResult.hasErrors()){
            throw new MethodArgumentNotValidException(null,bindingResult);
        }
        ///
        return userCommonService.getUsersListService(usersListReques);
    }



    ///新增管理员
    @ResponseBody
    @RequestMapping(value = "/admin/common/users/add",method = RequestMethod.POST)
    public Object usersAdd(@Valid @RequestBody PostUserAddRequest userAddRequest, BindingResult bindingResult,HttpServletRequest request) throws Exception{
        //如果校验存在错误
        if(bindingResult.hasErrors()){
            throw new MethodArgumentNotValidException(null,bindingResult);
        }
        ///
        return userCommonService.usersAddService(userAddRequest,request);
    }


    ///管理员上线下线
    @ResponseBody
    @RequestMapping(value = "/admin/common/users/saveStatus",method = RequestMethod.POST)
    public Object usersSaveStatus(@Valid @RequestBody PostUserSaveStatusRequest userSaveStatusRequest, BindingResult bindingResult, HttpServletRequest request) throws Exception{
        //如果校验存在错误
        if(bindingResult.hasErrors()){
            throw new MethodArgumentNotValidException(null,bindingResult);
        }
        ///
        return userCommonService.usersSaveStatusService(userSaveStatusRequest,request);
    }









}
